package www.worq.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import www.worq.controller.beans.registroBean;

public class registroDao extends oreqDao {

	public ArrayList<registroBean> getREGISTROS() throws SQLException {
		ArrayList<registroBean> registros = new ArrayList<registroBean>();
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select * from registro");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out
					.println("dao.registros.getREGISTROS.Ejecutado----------> "
							+ query.toString());

			while (rs.next()) {
				registroBean registro = new registroBean();
				registro.setId_registro(rs.getInt(1));
				registro.setId_recibo(rs.getInt(2));
				registro.setMatricula(rs.getString(3));
				registro.setFecha(rs.getString(4));
				registro.setTipo(rs.getString(5));
				registro.setCve_registro(rs.getInt(6));
				registros.add(registro);
			}
			System.out.println("dao.registro.getREGISTRO----------> Cargado");
		} catch (Exception e) {
			System.out.println("registroDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return registros;
	}

	public int nextId() {
		int id = 0;
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select id_registro from registro");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.registro.nextId.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				if (id < rs.getInt(1)) {
					id = rs.getInt(1);
				}
			}
			System.out.println("dao.registro.nextId----------> Cargado");
		} catch (Exception e) {
			System.out.println("registroDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return id + 1;
	}

	public void insertREGISTRO(registroBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("INSERT INTO registro ")
					.append("(id_registro, id_recibo, matricula, fecha, tipo, cve_registro) ")
					.append("values ('").append(bean.getId_registro())
					.append("','").append(bean.getId_recibo()).append("','")
					.append(bean.getMatricula()).append("','")
					.append(bean.getFecha()).append("','")
					.append(bean.getTipo()).append("','")
					.append(bean.getCve_registro()).append("');");
			System.out
					.println("dao.registro.insertREGISTRO.Ejecutado----------> "
							+ query.toString());
			st = conn.createStatement();
			st.executeUpdate(query.toString());

			System.out
					.println("dao.registro.insertREGISTRO----------> insertado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void deleteREGISTRO(registroBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("DELETE FROM registro where id_registro=").append(
					bean.getId_registro());

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.registro.deleteREGISTRO.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.registro.deleteREGISTRO----------> eliminado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void updateREGISTRO(registroBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("UPDATE faqs SET ").append("fecha='")
					.append(bean.getFecha()).append("',tipo='")
					.append(bean.getTipo()).append("',cve_registro='")
					.append(bean.getCve_registro()).append("' where id_faq=")
					.append(bean.getId_registro()).append(";");

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.registro.updateREGISTRO.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.registro.updateREGISTRO----------> actualizado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}
}
